package com.example.teaknower.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.teaknower.R;
import com.example.teaknower.adapter.ArticleRecycleViewAdapter;
import com.example.teaknower.model.article;
import com.example.teaknower.utils.OkHttpUtil;
import com.example.teaknower.utils.UserUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CollectionActivity extends AppCompatActivity {
    private RecyclerView recyclerView;//声明RecyclerView
    private ArticleRecycleViewAdapter recycleViewAdapter;//声明适配器
    private Context context;
    private List<article> mdata = new ArrayList<>();
    private MainActivity MainActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.collection_activity);

        setToolBar();

        recyclerView = (RecyclerView) findViewById(R.id.recycler_collection);

        //
        getDate();
        recycleViewAdapter = new ArticleRecycleViewAdapter(R.layout.article_item_view, mdata);
        MainActivity = com.example.teaknower.activity.MainActivity.getMainActivity();
        recycleViewAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                //Toast.makeText(MainActivity,"点击文章的id为 ： " + mdata.get(position).getId(),Toast.LENGTH_LONG).show();
                Log.e("经营中点击文章的id",""+mdata.get(position).getId());
                Intent intent = new Intent(MainActivity, ArticledetailsActivity.class);
                intent.putExtra("id",mdata.get(position).getId());
                MainActivity.startActivity(intent);
            }
        });
        /*
        与ListView效果对应的可以通过LinearLayoutManager来设置
        与GridView效果对应的可以通过GridLayoutManager来设置
        与瀑布流对应的可以通过StaggeredGridLayoutManager来设置
        */
        //LinearLayoutManager manager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        //RecyclerView.LayoutManager manager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
        //GridLayoutManager manager1 = new GridLayoutManager(context,2);
        //manager1.setOrientation(GridLayoutManager.VERTICAL);
        //StaggeredGridLayoutManager manager2 = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(recycleViewAdapter);

    }

    private void setToolBar() {
        Toolbar toolbar =  findViewById(R.id.shoucang_toolbar);
        setTitleCenter(toolbar);
        this.setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//        this.getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

    //Toolbar的事件---返回
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
    /*
     * 设置标题居中
     */
    public void setTitleCenter(Toolbar toolbar) {
        String title = "title";
        final CharSequence originalTitle = toolbar.getTitle();
        toolbar.setTitle(title);
        for (int i = 0; i < toolbar.getChildCount(); i++) {
            View view = toolbar.getChildAt(i);
            if (view instanceof TextView) {
                TextView textView = (TextView) view;
                if (title.equals(textView.getText())) {
                    textView.setGravity(Gravity.CENTER);
                    Toolbar.LayoutParams params = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.MATCH_PARENT);
                    params.gravity = Gravity.CENTER;
                    textView.setLayoutParams(params);
                }
            }
            toolbar.setTitle(originalTitle);
        }
    }

    private void getDate(){
        String s = null;
        UserUtil userUtil = new UserUtil(com.example.teaknower.activity.MainActivity.getMainActivity());
        String Uid = UserUtil.getUserId();
        try {
            s = OkHttpUtil.getRequest("http://192.168.3.25:8080/Collection/findCollectionByUserId?id=8" /*+ Uid*/);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject;
        jsonObject = JSONObject.parseObject(s);
        List<Object> mList = (List<Object>)jsonObject.get("data");
        for(int i = 0 ; i < mList.size() ; i++ ){
            Map<String,Object> m = (Map<String,Object>) mList.get(i);
            String id = m.get("article_id").toString();
            String date = m.get("date").toString();
            String img_url = m.get("img_url").toString();
            String title = m.get("article_title").toString();
            article article = new article(id, title, img_url, date);
            mdata.add(article);
        }
    }

}
